package main

import (
	"fmt"
	"iot-base/common/cache"
	"iot-base/common/logger"
	"sync"
)

type RegisterSvManager struct {
	m            sync.Mutex
	RegisterMap  map[string]func(id, brand string)
	RegisterSv   chan string
	UnRegisterSv chan string
}

//初始化用于注册启动不同品牌型号的服务，统一进行管理
var Register RegisterSvManager
var RegsiterFunc = map[string]func(id, brand string){
	NVFBrand:  NewVersionFaceRegister,
	HaiKBrand: HaiKRegister,
}

var UnRegisterFunc = map[string]func(id string){
	NVFBrand:  NewVersionUnRegister,
	HaiKBrand: HaiKUnRegister,
}

func initRegister() {
	Register = RegisterSvManager{
		m:           sync.Mutex{},
		RegisterMap: make(map[string]func(id, brand string)),
	}
}

//所有品牌使用的注册分为两部分定义，需要brand的型号，固定参数配置根据物模型使用的设备参数进行提取
func (m *RegisterSvManager) Register(id, brand string) {
	if _, ok := m.RegisterMap[brand]; !ok {
		if fun, ok := RegsiterFunc[brand]; ok {
			m.RegisterMap[brand] = fun
			go fun(id, brand)
		}
	} else {
		for k, _ := range m.RegisterMap {
			if k != brand {
				delete(m.RegisterMap, k)
				m.UnRegister(id, brand)
			}
		}
	}
}

func (m *RegisterSvManager) UnRegister(id, brand string) {
	UnRegisterFunc[brand](id)
}

func registDoor(id string, thing *cache.ThingInfo, v interface{}, round int64) (interface{}, error) {
	spec := thing.GetThingParam("spec", "")
	Register.Register(id, spec)
	logger.Log.Info("register:" + fmt.Sprint(Register))
	return nil, nil
}
